//
//  ModifyUserSexViewController.m
//  WeCommunity
//
//  Created by mac on 2017/7/20.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "ModifyUserSexViewController.h"
#import "ChooseTableViewCell.h"
#import "ChooseSexModel.h"
#import "MineListSpaceCell.h"

#define NULL_ROW1_Height 16
#define Row_Height 44

@interface ModifyUserSexViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView * tableView;

@property (nonatomic,strong) NSMutableArray * dataSource;

@end

@implementation ModifyUserSexViewController


#pragma mark - get
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,KScreen_Width, KScreen_Height - KNaviheight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor colorWithHex:0xf8f8f8 alpha:1.0];
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self initWithUserInterface];
}

-(void)initWithUserInterface
{
    self.view.backgroundColor = [UIColor colorWithHex:0xefeff4 alpha:1.0];
    
    self.title = KNSLocal(@"Sex");
    
    self.dataSource = [NSMutableArray array];
    [self.dataSource addObject:@"0"];
    for (int i = 0; i < 2; i++) {
        ChooseSexModel * model = [ChooseSexModel new];
        model.sex = [NSString stringWithFormat:@"%d",i + 1];
        [self.dataSource addObject:model];
        if ([model.sex isEqualToString:[UserInfoManger shared].contact.sex]) {
            model.choose = YES;
        }else{
            model.choose = NO;
        }
    }
    
    [self.view addSubview:self.tableView];
    [self.tableView reloadData];

}

#pragma mark - tableView delegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id object = self.dataSource[indexPath.row];
    if ([object isKindOfClass:[NSString class]]) {
        return NULL_ROW1_Height;
    }else{
        return Row_Height;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    id  object = self.dataSource[indexPath.row];

    if ([object isKindOfClass:[NSString class]]) {
        //隔行cell
        static NSString * cellID = @"mineListSpaceCell";
        MineListSpaceCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell =[[MineListSpaceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        //没有点击效果
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        static NSString * cellID = @"chooseSexCell";
        ChooseTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell =[[ChooseTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        
        [cell creatCellWithModel:object];
        
        if (indexPath.row == 1) {
            [cell needAddTopLine:YES addBottomLine:NO];
        }else if(indexPath.row == 2){
            [cell needAddTopLine:NO addBottomLine:YES];
        }
        
        return cell;
    }
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        return;
    }
    
    ChooseSexModel * model = self.dataSource[indexPath.row];
    if (model.choose) {
        return;
    }else{
        [self showLoadingHudWithHText:nil];
        @weakify(self);
        [[UserInfoManger shared] saveUserSex:model.sex complete:^(BOOL success,NSString * error) {
            [self hiddenLoadingHud];
            @strongify(self);
            if (success) {
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [UITool showHudWithText:error withView:self.view];
            }
        }];
    }
}



@end
